package com.yanqu.road.dao.impl.activity.xiuxian;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.xiuxian.data.CrossXiuXianMountAllotData;
import com.yanqu.road.entity.activity.xiuxian.data.XiuXianUserMountAllotData;
import com.yanqu.road.entity.activity.xiuxian.data.XiuXianUserMountInfoData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossXiuXianMountAllotDataDaoImpl extends TempDataObjectDao<CrossXiuXianMountAllotData> {

    public static String getCrossXiuXianMountAllotDataSql = "select * from `t_c_xiuxian_mount_allot_data` where `activityId`=?";
    public Map<Long, CrossXiuXianMountAllotData> getCrossXiuXianMountAllotData(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, CrossXiuXianMountAllotData> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getCrossXiuXianMountAllotDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossXiuXianMountAllotData data = parse(rs);
                map.put(data.getAllotTimeId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateCrossXiuXianMountAllotDataSql = "update `t_c_xiuxian_mount_allot_data` set ackServerIdList=? where activityId=? and allotTimeId=?";
    @Override
    protected boolean updateData(CrossXiuXianMountAllotData crossXiuXianMountAllotData) {
        crossXiuXianMountAllotData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossXiuXianMountAllotData.getAckServerIdList())));
        para.put(i++, new DbParameter(Types.INTEGER, crossXiuXianMountAllotData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossXiuXianMountAllotData.getAllotTimeId()));
        if (getDb().executeNoneQuery(updateCrossXiuXianMountAllotDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossXiuXianMountAllotDataSql = "insert into `t_c_xiuxian_mount_allot_data`(activityId,allotTimeId,ackServerIdList) values (?,?,?)";
    @Override
    protected boolean addData(CrossXiuXianMountAllotData crossXiuXianMountAllotData) {
        crossXiuXianMountAllotData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossXiuXianMountAllotData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossXiuXianMountAllotData.getAllotTimeId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossXiuXianMountAllotData.getAckServerIdList())));
        if (getDb().executeNoneQuery(insertCrossXiuXianMountAllotDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossXiuXianMountAllotData parseData(ResultSet rs) throws SQLException {
        CrossXiuXianMountAllotData crossXiuXianMountAllotData = new CrossXiuXianMountAllotData();
        crossXiuXianMountAllotData.setActivityId(rs.getInt("activityId"));
        crossXiuXianMountAllotData.setAllotTimeId(rs.getLong("allotTimeId"));
        crossXiuXianMountAllotData.setAckServerIdList(JSON.parseObject(rs.getString("ackServerIdList"), new TypeReference<List<Long>>() {}));

        return crossXiuXianMountAllotData;
    }
}
